package com.flashcash.kotlin.activity

import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.TextView
import com.flashcash.kotlin.data.PolicyType
import com.flashcash.solu.BuildConfig
import com.flashcash.solu.R
import com.flashcash.solu.app.base.BaseActivity
import com.flashcash.solu.app.base.BaseActivityView
import com.flashcash.solu.app.base.presenter.BaseActivityPresenter
import com.flashcash.solu.app.base.presenter.BaseActivityPresenterImpl
import com.flashcash.solu.view.fragment.LoaningFragment
import org.jetbrains.anko.find
import org.jetbrains.anko.sdk25.coroutines.onClick

/**
 * Created by XLEO on 2018/1/4.
 */
class PolicyActivity : BaseActivity<PolicyActivityPresenter>(), PolicyActivityView {

    companion object {
        const val RESULT_CODE = 800
    }

    override fun getLayoutResId(): Int {
        return R.layout.activity_policy
    }

    override fun init() {
        val type = intent.extras.getString(LoaningFragment.KEY_AGREE)
        if (type == null) {
            finish()
        }
        val productId = intent.extras.getString(LoaningFragment.KEY_LOAN_APP_ID)
        if (productId == null) {
            finish()
        }
        find<View>(R.id.id_imagebutton_info_list).visibility = View.GONE
        find<View>(R.id.id_imagebutton_back).onClick {
            finish()
        }
        find<View>(R.id.btn_accept).onClick {
            setResult(RESULT_CODE)
            finish()
        }
        find<TextView>(R.id.id_textview_title).apply {
            text = when (type) {
                PolicyType.PRIVACY.name -> {
                    getString(R.string.textview_me_policy)
                }
                PolicyType.LOAN_AGREEMENT.name -> {
                    getString(R.string.title_loan_agreement_title)
                }
                else -> {
                    throw IllegalArgumentException("unknown policy type")
                }
            }
        }
        find<WebView>(R.id.wv).apply {
            settings.setAppCachePath(cacheDir.absolutePath)
            settings.setAppCacheEnabled(true)
            settings.setSupportZoom(true)
            settings.javaScriptEnabled = true
            webChromeClient = object : WebChromeClient() {
                override fun onProgressChanged(view: WebView?, newProgress: Int) {
                    if (newProgress >= 99) {

                        dismissLoading()
                    }
                }

            }
            webViewClient = object : WebViewClient() {
                override fun onPageFinished(view: WebView?, url: String?) {
                    dismissLoading()
                }
            }
            when (type) {
                PolicyType.PRIVACY.name -> {
                    loadUrl(BuildConfig.AGREEMENT_URL)
                }
                PolicyType.LOAN_AGREEMENT.name -> {
//                    loadUrl(BuildConfig.LOAN_AGREEMENT)
                    loadUrl(BuildConfig.CONTRACT_URL + "?loanId=" + productId)
                }
                else -> {
                    throw IllegalArgumentException("unknown policy type")
                }
            }
        }
        showLoading("")
    }

    override fun initPresenterImpl(): PolicyActivityPresenter {
        return PolicyActivityPreImpl()
    }

}

class PolicyActivityPreImpl : BaseActivityPresenterImpl(), PolicyActivityPresenter {

}

interface PolicyActivityPresenter : BaseActivityPresenter {

}

interface PolicyActivityView : BaseActivityView {

}
